﻿using FreeDream.Common;
using FreeDream.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FreeDream.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ReceivedCacheController : ControllerBase
    {
        #region Fields
        private readonly ILogger<ReceivedCacheController> _logger;
        public ReceivedCacheController(ILogger<ReceivedCacheController> logger)
        {
            _logger = logger;
        }
        #endregion

        #region 1.0 提供给设备推送信息缓存
        /// <summary>
        /// 提供给设备推送信息缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Receive(string key, [FromBody] object obj)
        {
            BaseResponse br = new BaseResponse();
            #region 1.0 存储Keys 如果CacheKeys为空
            List<string> keys = CacheHelper2.CacheValue("CacheKeys") as List<string>;
            if (keys == null)
            {
                keys = new List<string>();
            }
            //如果key存在
            if (!keys.Contains(key))
            {
                keys.Add(key);
            }
            CacheHelper2.CacheInsert("CacheKeys", keys);
            #endregion
            //var obj1 = JsonConvert.DeserializeObject<dynamic>(obj.ToString());

            CacheHelper2.CacheInsert(key, obj);
            br.Message = "接收缓存消息成功";
            return Ok(br);
        }
        #endregion

        #region 2.0 删除指定键缓存
        /// <summary>
        /// 删除指定键缓存
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ClearCacehData(string key)
        {
            BaseResponse br = new BaseResponse();
            List<string> keys = CacheHelper2.CacheValue("CacheKeys") as List<string>;
            if (keys != null && keys.Count != 0)
            {
                keys.Remove(key);
                CacheHelper2.CacheInsert("CacheKeys", keys);
            }
            CacheHelper2.CacheNull(key);
            br.Message = $"删除缓存[{key}]成功";
            return Ok(br);
        }
        #endregion

        #region 3.0 删除全部缓存
        /// <summary>
        /// 删除全部缓存
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DeleteAllCache()
        {
            BaseResponse br = new BaseResponse();
            List<string> keys = CacheHelper2.CacheValue("CacheKeys") as List<string>;
            if (keys == null)
            {
                br.SetFailed("没有任何缓存删除失败");
                return Ok(br);
            }

            foreach (var key in keys)
            {
                CacheHelper2.CacheNull(key);
            }
            CacheHelper2.CacheNull("CacheKeys");
            return Ok(br);
        }
        #endregion

        #region 4.0 查询全部缓存
        /// <summary>
        /// 查询全部缓存
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult QueryAllCache()
        {
            BaseResponse br = new BaseResponse();
            List<string> keys = CacheHelper2.CacheValue("CacheKeys") as List<string>;
            if (keys == null)
            {
                br.SetFailed("当前没有缓存信息");
                return Ok(br);
            }
            Dictionary<string, object> dict = new Dictionary<string, object>();
            foreach (var key in keys)
            {
                var value = CacheHelper2.CacheValue(key);
                if (value != null)
                {
                    dict.Add(key, value);
                }
            }
            br.Data = dict;
            return Ok(br);
        }
        #endregion

        #region 5.0 查询全部缓存
        /// <summary>
        /// 查询全部缓存
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult QueryCacheByKey(string key)
        {
            BaseResponse br = new BaseResponse();
            var value = CacheHelper2.CacheValue(key);
            if (value == null)
            {
                br.SetFailed("未查询到该键对应数据");
                return Ok(br);
            }
            br.Data = value;
            return Ok(br);
        }
        #endregion
    }
}
